import 'package:flutter_boost/flutter_boost.dart';
import 'package:t3_data_service/biz_data_source/map/address/poi_search/poi_search.dart';
import 'package:uikit/uikit.dart';
import '../model/absorbed_end_point.dart';

class BottomSelectedPOIViewModel extends ViewModel {
  var hiddenBottomView = false.obs;
  updateHiddenBottomView(bool hidden) {
    hiddenBottomView.value = hidden;
  }

  var buttonEnable = false.obs;
  updateButtonEnable(bool enable) {
    buttonEnable.value = enable;
  }

  var singlePointPOI = POIModel(location: const LatLng(0, 0)).obs;
  updateSinglePOI(POIModel model) {
    singlePointPOI.value = model;
    selectedPoint.value = T3AbsorbedEndPoint(index: -1, name: '');
  }

  var points = <T3AbsorbedEndPoint>[].obs;
  updateMultiPoints(POIModel model, List<T3AbsorbedEndPoint> endPoints) {
    singlePointPOI.value = model;
    points.value = endPoints;

    for (var element in endPoints) {
      if (pointIsEqual(element.coordinate, model.location)) {
        selectedPoint.value = element;
      }
    }
  }

  var selectedPoint = T3AbsorbedEndPoint(index: -1, name: '').obs;

  bool pointIsEqual(LatLng? left, LatLng? right) {
    if (left != null && right != null) {
      var leftLat = left.lat.toStringAsFixed(6);
      var leftLng = left.lng.toStringAsFixed(6);

      var rightLat = right.lat.toStringAsFixed(6);
      var rightLng = right.lng.toStringAsFixed(6);
      return leftLat == rightLat && leftLng == rightLng;
    }
    return false;
  }
}

extension POIModelValid on POIModel {
  bool get isValid => location.lat != 0 && location.lng != 0;
}